package com.umeox.ota.ui.view.centerView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;

import com.abupdate.iot_libs.engine.otaStatus.OtaStatus;
import com.abupdate.iot_libs.engine.otaStatus.OtaStatusMgr;
import com.abupdate.trace.Trace;
import com.umeox.ota.R;
import com.umeox.ota.data.local.RecordData;
import com.umeox.ota.engine.FotaInitTask;
import com.umeox.ota.gloable.App;
import com.umeox.ota.ui.activity.MainActivity;
import com.umeox.ota.utils.ActivityCollector;
import com.umeox.ota.utils.Utils;


/**
 * Created by raise.yang on 2016/04/01.
 */
public class CenterCircleView extends RelativeLayout {

    private static final String TAG = "CenterCircleView";
    private static OtaStatus otaStatusTemp = OtaStatus.IDLE;

    static CommonCircleView m_circle_view;
    static ImageView m_arrow;
    static TextView m_progress_view;
    static TextView m_state_detail_view;

    public CenterCircleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CenterCircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.viewgroup_circle, this);
        m_circle_view = (CommonCircleView) findViewById(R.id.view_circle);
        m_circle_view.setTag(R.id.tag_check_id);
        m_arrow = (ImageView) findViewById(R.id.image_arrow);
        m_progress_view = (TextView) findViewById(R.id.tv_progress);
        m_state_detail_view = (TextView) findViewById(R.id.tv_update_state_detail);

        m_circle_view.loadXmlAnim(R.anim.iot_rotate);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        super.setOnClickListener(l);
        m_circle_view.setOnClickListener(l);
    }

    public class Status {
        /*
        查询本地版本
         */
        public static final int STATE_QUERYLOCALVERSION = 0;

        /*
        查询服务器新版本
         */
        public static final int STATE_QUERYNEWVERSION = 1;

        /*
        查询到新版本
         */
        public static final int STATE_NEWVERSION_READY = 2;

        /*
        下载中
         */
        public static final int STATE_DOWNLOADING = 3;

//        /*
//        下载暂停
//         */
//        public static final int STATE_PAUSEDOWNLOAD = 4;

        /*
        下载完成
         */
        public static final int STATE_DLPKGCOMPLETE = 5;

        /*
        检测中
         */
        public static final int STATE_CHECKING = 6;

        /*
        取消下载
         */
        public static final int STATE_CANCELDOWNLOAD = 7;

        /*
         下载失败
          */
        public static final int STATE_FAILEDDOWNLOAD = 9;


        /*
        升级
         */
        public static final int STATE_UPGRADE = 8;

        /*
        升级失败
         */
        public static final int STATE_FAILUPGRADE = 10;

        /**
         * 升级中
         */
        public static final int STATE_UPGRADING = 11;

        /**
         * 升级成功
         */
        public static final int STATE_UPGRADE_SUCCESS = 12;

        /**
         * need reboot
         */
        public static final int STATE_NEED_REBOOT = 13;
    }

    @BindingAdapter({"status"})
    public static void setStatus(View view, OtaStatus status) {
        Trace.d(TAG, "setStatus() :" + status);
        if (status == null) {
            return;
        }
        otaStatusTemp = status;
        switch (status) {
            case IDLE:
                refreshStatus(Status.STATE_QUERYNEWVERSION);
                break;

            case CHECKING:
                refreshStatus(Status.STATE_CHECKING);
                break;

            case CHECK_NEW_VERSION:
                refreshStatus(Status.STATE_NEWVERSION_READY);
                break;

            case UPGRADING:
                refreshStatus(Status.STATE_UPGRADING);
                break;

            case DOWNLOADING:
                refreshStatus(Status.STATE_DOWNLOADING);
                break;

            case UPGRADE_PAUSE:
                refreshStatus(Status.STATE_DLPKGCOMPLETE);
                break;

            case DOWNLOAD_FINISHED:
                refreshStatus(Status.STATE_DLPKGCOMPLETE);
                break;

            case DOWNLOAD_PAUSE:
                refreshStatus(Status.STATE_NEWVERSION_READY);
                break;

            case DOWNLOAD_FAILED:
                refreshStatus(Status.STATE_NEWVERSION_READY);
                break;

            case UPGRADE_FAIL:
                refreshStatus(Status.STATE_FAILUPGRADE);
                break;

            case UPGRADE_SUCCESS:
                refreshStatus(Status.STATE_UPGRADE_SUCCESS);
                break;

            case NEED_REBOOT:
                refreshStatus(Status.STATE_NEED_REBOOT);
                break;

            default:
                break;
        }
    }

    @BindingAdapter({"progress"})
    public static void setProgress(View view, int progress) {
        if (!FotaInitTask.isCompleted()) {
            Trace.d(TAG, "Init not completed");
            return;
        }
        OtaStatus otaStatus = OtaStatusMgr.getInstance().getCurStatus();
        Trace.d(TAG, "ota status: " + otaStatus.name() + ", progress: " + progress);
        Trace.d(TAG, "otaStatusTemp: " + otaStatusTemp.name() + ", progress: " + progress);
        switch (otaStatus) {
            case DOWNLOADING:
                if (progress <= 0) {
                    if (RecordData.downloadProgress > 0) {
                        progress = RecordData.downloadProgress;
                    } else {
                        progress = 0;
                    }
                }
                if (otaStatusTemp != OtaStatus.DOWNLOADING) {
                    progress = 0;
                }
                m_progress_view.setText(progress + "%");
                m_circle_view.setProgress(progress);
                break;

            case UPGRADING:
                if (progress <= 0) {
                    if (RecordData.upgradeProgress > 0) {
                        progress = RecordData.upgradeProgress;
                    } else {
                        progress = 0;
                    }
                }
                m_progress_view.setText(progress + "%");
                m_circle_view.setProgress(progress);
                break;

            case CHECKING:
                if (otaStatusTemp == OtaStatus.IDLE) {
                    m_circle_view.setProgress(0);
                } else {
                    m_circle_view.setProgress(10);
                }
                break;

            case IDLE:
                if (otaStatusTemp == OtaStatus.UPGRADING) {
                    m_progress_view.setText(0 + "%");
                }
                m_circle_view.setProgress(0);
                break;

            case DOWNLOAD_FINISHED:
            case CHECK_NEW_VERSION:
                if (otaStatusTemp == OtaStatus.UPGRADING
                        || otaStatusTemp == OtaStatus.DOWNLOADING) {
                    m_progress_view.setText("0%");
                    m_circle_view.setProgress(0);
                } else {
                    m_circle_view.setProgress(100);
                }
                break;
            case DOWNLOAD_PAUSE:
                m_progress_view.setText("0%");
                m_circle_view.setProgress(0);
                break;
            case UPGRADE_SUCCESS:
            case UPGRADE_FAIL:
            case DOWNLOAD_FAILED:
            case UPGRADE_PAUSE:
            case NEED_REBOOT:
                m_circle_view.setProgress(100);
                break;
            default:
                m_circle_view.setProgress(10);
                break;
        }
    }

    public static void refreshStatus(int status) {
        if (!FotaInitTask.isCompleted()) {
            Trace.d(TAG, "Init not completed");
            return;
        }
        switch (status) {
            case Status.STATE_QUERYLOCALVERSION:
                Trace.d(TAG, "refreshStatus() STATE_QUERY_LOCAL_VERSION");
                refreshView(false, 10, true, INVISIBLE, 0, App.sCx.getString(R.string.tips_fota_loading), GONE, null);
                break;

            case Status.STATE_QUERYNEWVERSION:
            case Status.STATE_FAILUPGRADE:
                //无状态，可以点击检测版本
                Trace.d(TAG, "refreshStatus() STATE_QUERY_NEW_VERSION");
                refreshView(true, 100, false, VISIBLE, 0, App.sCx.getString(R.string.check_version), GONE, R.id.tag_check_id);
                break;

            case Status.STATE_CHECKING:
                //检测版本中
                Trace.d(TAG, "refreshStatus() STATE_CHECKING");
                refreshView(false, 10, true, INVISIBLE, 0, App.sCx.getString(R.string.checking), GONE, null);
                break;

            case Status.STATE_NEWVERSION_READY:
                //检测到新版本，可以点击下载
                Trace.d(TAG, "refreshStatus() STATE_NEW_VERSION_READY");
                refreshView(true, 100, false, VISIBLE, 180, App.sCx.getString(R.string.download), GONE, R.id.tag_download_id);
                break;

            case Status.STATE_CANCELDOWNLOAD:
                //取消下载
                Trace.d(TAG, "refreshStatus() STATE_CANCEL_DOWNLOAD");
                refreshView(false, 100, false, VISIBLE, 0, App.sCx.getString(R.string.check_version), GONE, R.id.tag_check_id);
                break;

            case Status.STATE_DOWNLOADING:
                //下载中，可以点击取消下载按钮
                Trace.d(TAG, "refreshStatus() STATE_DOWNLOADING");
                refreshView(false, -1, false, GONE, 0, null, VISIBLE, null);
                break;

            case Status.STATE_DLPKGCOMPLETE:
                //下载完成，可以点击升级
                Trace.d(TAG, "refreshStatus() STATE_DLPKG_COMPLETE");
                refreshView(true, 100, false, VISIBLE, 0, App.sCx.getString(R.string.click_update), GONE, R.id.tag_update_now_id);
                break;

            case Status.STATE_FAILEDDOWNLOAD:
                //下载失败
                Trace.d(TAG, "refreshStatus() STATE_FAILED_DOWNLOAD");
                refreshView(true, 100, false, VISIBLE, 0, App.sCx.getString(R.string.check_version), GONE, R.id.tag_check_id);
                break;

            case Status.STATE_UPGRADE:
            case Status.STATE_UPGRADING:
                //升级中
                Trace.d(TAG, "refreshStatus() STATE_UPGRADE");
                if (Utils.isSupportAbupdate()) {
                    //AB 升级中 UI 有差别
                    refreshView(false, -1, false, INVISIBLE, 0, App.sCx.getString(R.string.updating), VISIBLE, null);
                } else {
                    refreshView(false, 20, true, INVISIBLE, 0, App.sCx.getString(R.string.updating), GONE, null);
                }
                break;
            case Status.STATE_UPGRADE_SUCCESS:
                Trace.d(TAG, "refreshStatus() STATE_UPGRADE_SUCCESS");
                refreshView(true, 100, false, VISIBLE, 0, App.sCx.getString(R.string.click_reboot), GONE, R.id.tag_reboot_id);
                break;
            case Status.STATE_NEED_REBOOT:
                Trace.d(TAG, "refreshStatus() STATE_NEED_REBOOT");
                refreshView(true, 100, false, VISIBLE, 0, App.sCx.getString(R.string.click_reboot), INVISIBLE, R.id.tag_reboot_id);
                MainActivity mainActivity = ActivityCollector.getActivity(MainActivity.class);
                if (mainActivity != null) {
                    mainActivity.createChooseAbupdateDialog();
                }
                break;
            default:
                break;
        }
    }

    public void showUpgrading() {
        Trace.d(TAG, "showUpgrading()");
        refreshView(false, 10, true, INVISIBLE, 0, App.sCx.getString(R.string.updating), GONE, null);
    }

    public void showDownloadPrepare() {
        Trace.d(TAG, "show Download Prepare");
        refreshView(false, 10, true, INVISIBLE, 0, App.sCx.getString(R.string.download_prepare), GONE, null);
    }

    public void showQueryLocalInfo() {
        Trace.d(TAG, "show Query Local Info");
        refreshView(false, 0, false, INVISIBLE, 0, App.sCx.getString(R.string.tips_fota_loading), GONE, null);
    }

    public static void refreshView(boolean viewEnable, int progress, boolean needAnim, int arrowVisibility, int arrowRotation, @Nullable String tips, int progressVisibility, @Nullable Integer tagId) {
        m_circle_view.setEnabled(viewEnable);
        m_circle_view.setClickable(viewEnable);
        if (progress >= 0) {
            m_circle_view.setProgress(progress);
        }
        m_circle_view.setTag(tagId);
        if (needAnim) {
            m_circle_view.startAnim();
        } else {
            m_circle_view.stopAnim();
        }
        m_arrow.setVisibility(arrowVisibility);
        m_arrow.setRotation(arrowRotation);
        m_progress_view.setVisibility(progressVisibility);
        m_state_detail_view.setText(tips);
    }

    public void resetProgress() {
        m_circle_view.setProgress(0);
    }
}
